<?php

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

//index db
require_once '../../index.php';
require_once '../../utils.php';
require_once 'functional.php';

//init properties
$params = array();
$post_arr = array();

//receive properties
$params['identify'] = $_GET['identify'];
$params['hash'] = $_GET['hash'];

//identify process
if (!identify($db, 'users', $params)) {
  json_encode(array('error' => 'No identify.'), JSON_PRETTY_PRINT);
  return;
}

//read users
$result = read($db, 'users');

//get the row count
$num = $result->rowCount();

if($num > 0) {
  $post_arr['data'] = array();

  while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    extract($row);
    $post_item = array(
      'id' => $id,
      'rol' => $rol,
      'name' => $name,
      'softDelete' => $softDelete,
      'createdAt' => $createdAt
    );
    array_push($post_arr['data'], $post_item);
  }

  //convert to JSON and output
  echo json_encode($post_arr, JSON_PRETTY_PRINT);
}
else {
  echo json_encode(array('error' => 'No post found.'), JSON_PRETTY_PRINT);
}

?>